<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>固定导航栏位置</title>
    <style>
        *{
            padding:0;
            margin:0;
        }

        /*.top,.nav{*/
            /*width: 1423px;*/
            /*margin: 0 auto;*/
        /*}*/
        img{
            vertical-align: top;
            width: 100%;
        }

        .main{
            width: 1001px;
            margin: 0 auto;
        }

        .fixed{
            position: fixed;
            width: 100%;
            top: 0;
        }
    </style>
</head>
<body>
    <div class="top">
        <img src="images/top.png" alt="">
    </div>
<div class="nav" id="Q-nav">
    <img src="images/nav.png" alt="">
</div>

<div class="main" id="main">
    <img src="images/main.png" alt="">
</div>
</body>
</html>

<script src="my.js" type="text/javascript"></script>
<script>
    var nav = $("Q-nav");
    var main = $("main");

    var navTop = nav.offsetTop;
    window.onscroll = function () {
        if (scroll().top > navTop){
            nav.className = "nav fixed";
            main.style.paddingTop = nav.offsetHeight + 'px';
        }else {
            nav.className = "nav";
            main.style.paddingTop = 0;
        }
    }

</script>
